package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.domain.hdw.CabinetProperty;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.webserver.persistence.hdw.CabinetPropertyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * 充电柜属性业务
 * 
 * @Date:Created in 2018/7/6 14:59
 * @Author:
 * @Modified By: zjn 2018/7/6 14:59
 */ 
@Service
public class CabinetPropertyService {
    @Autowired
    CabinetPropertyMapper cabinetPropertyMapper;
    /**
    * 
    * 根据柜子id查询
    * @Param: [cabinetId]
    * @Return: java.util.List<cc.yugu.yhdw.common.domain.hdw.CabinetProperty>
    * @Author By: zjn 2018/7/6  15:01
    */ 
    public List<CabinetProperty> findByCabinet(String cabinetId) {
        return cabinetPropertyMapper.findByCabinet(cabinetId);
    }
    /**
    * 插入
    * 
    * @Param: [cabinetId, active, property, value]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/6  15:01
    */ 
    @Transactional(rollbackFor=Throwable.class)
    public ExtResult insert(String cabinetId, int[] active, String[] property, String[] value) {
        int effect = 0;
        // 根据柜子id删除
        cabinetPropertyMapper.deleteByCabinet(cabinetId);
        if(active != null && property != null && value != null) {

            for(int i = 0; i < active.length; i++) {
                CabinetProperty record = new CabinetProperty();
                record.setOrderNum(i + 1);
                record.setCabinetId(cabinetId);
                record.setActiveFlag(active[i]);
                record.setPropertyName(property[i]);
                record.setPropertyValue(value[i]);

                cabinetPropertyMapper.insert(record);
                effect++;
            }
        }
        if (effect == 0) {
            return ExtResult.failResult("操作失败");
        } else {
            return ExtResult.successResult();
        }
    }

}
